package com.atts.baccarat.table.impl;

import java.util.List;

import com.atts.baccarat.table.BaccaratTable;
import com.atts.baccarat.table.Table;
import com.atts.baccarat.table.TableItem;

/**
 * 垂直顺序表格， 例如珠盘路
 * 
 * @author Administrator
 * 
 */
public abstract class VerticalSeqTable extends Table implements BaccaratTable {

	/**
	 * 元素个数
	 */
	private int itemCount;

	public VerticalSeqTable(final int rowCount) {
		super(rowCount);
	}

	@Override
	public void add(final TableItem item) {

		if (item != null && !this.isValidItem(item)) {
			throw new IllegalArgumentException(
			"TableItem is invalid for this table");
		}

		this.itemCount++;

		final int columnIndex = (this.itemCount - 1) / super.getRowCount();
		final int rowIndex = (this.itemCount - 1) % super.getRowCount();

		super.addItem(rowIndex, columnIndex, item);
	}

	/**
	 * 清空数据
	 */
	@Override
	public void clear() {

		super.clear();
		this.itemCount = 0;
	}

	@Override
	public void previous() {

		if (this.itemCount == 0) {
			return;
		}

		final int columnIndex = (this.itemCount - 1) / super.getRowCount();
		final int rowIndex = (this.itemCount - 1) % super.getRowCount();

		super.addItem(rowIndex, columnIndex, null);

		this.itemCount--;
	}

	@Override
	public TableItem getLast() {

		if (this.itemCount == 0) {
			return null;
		}

		final int columnIndex = (this.itemCount - 1) / super.getRowCount();
		final int rowIndex = (this.itemCount - 1) % super.getRowCount();

		return super.getItem(rowIndex, columnIndex);
	}

	@Override
	public List<List<TableItem>> getMatrixForDisplay() {

		return super.getMatrix();
	}
}
